<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <input type="text" class="user">
    <br>
    <input type="text" class="pwd">
    <br>
    <button>注册</button>


    <script>

        const oUser = get('.user');
        const oPwd = get('.pwd');
        const oBtn = get('button');

        function get(ele) {
            return document.querySelector(ele)
        }


        oBtn.onclick = function () {
            const username = oUser.value;
            const password = oPwd.value;

            const xhr = new XMLHttpRequest();
            xhr.open('post', '../php/reg.php', true);


            // 添加请求头   添加数据格式
            xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');

            // post请求通过send携带数据   名称=值&名称=值
            xhr.send('username=yy&password=123');
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    let data = xhr.responseText;
                    console.log(data);
                }
            }
        }




    </script>

</body>

</html>